<script setup lang="ts">
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import { getHomeBanner, getHomeCategory, getHomeHot } from '@/services/home'
import CustomNavbar from './components/CustomNavbar.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
import XtxSwiper from '@/components/XtxSwiper.vue'
import XtxGuess from '@/components/XtxGuess.vue'

const bannerList = ref<BannerItem[]>([])
const categoryList = ref<CategoryItem[]>([])
const hotList = ref<HotItem[]>([])
const guessRef = ref() // 获取暴露成员
// 下拉刷新状态
const isTriggered = ref(false)

// 轮播图
const getHomeBannerData = async () => {
  const res = await getHomeBanner()
  bannerList.value = res.result
  console.log('轮播图', res)
}

// 分类数据
const getCategoryData = async () => {
  const res = await getHomeCategory()
  categoryList.value = res.result
  console.log('分类', res)
}

// 热门数据
const getHomeHotData = async () => {
  const res = await getHomeHot()
  hotList.value = res.result
  console.log('热门', res)
}

// 自定义下拉刷新被触发
const onRefresherrefresh = async () => {
  console.log('下拉刷新执行了')
  // 开启刷新动画
  isTriggered.value = true
  guessRef.value?.resetData() // 加载数据
  await Promise.all([getHomeBannerData(), getCategoryData(), getHomeHotData()])
  // 关闭刷新动画
  isTriggered.value = false
}

onLoad(() => {
  getHomeBannerData()
  getCategoryData()
  getHomeHotData()
})
</script>

<template>
  <CustomNavbar />
  <scroll-view
    scroll-y
    refresher-enabled
    @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="isTriggered"
  >
    <!-- 轮播图 -->
    <XtxSwiper :bannerList="bannerList" />
    <CategoryPanel :categoryList="categoryList" />
    <HotPanel :hotList="hotList" />
    <XtxGuess ref="guessRef" />
  </scroll-view>
</template>

<style lang="scss">
page {
  background-color: #f7f7f7;
}
</style>
